package com.zjdiepu.www.biz.cw;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.cw.CwIncomeExpensesItemMapper;
import com.zjdiepu.www.ibiz.cw.ICwIncomeExpensesItemService;
import com.zjdiepu.www.model.cw.dto.CwIncomeExpensesItem;
import com.zjdiepu.www.model.cw.ext.CwIncomeExpensesItemExt;

/**
 * 收支管理明细Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年05月15日 18时13分<br/>
 * @since 1.0<br/>
 */
@Service
public class CwIncomeExpensesItemServiceImpl extends BaseService implements ICwIncomeExpensesItemService {
	public static final Logger logger = LoggerFactory.getLogger(CwIncomeExpensesItemServiceImpl.class);

	@Autowired
	private CwIncomeExpensesItemMapper cwIncomeExpensesItemMapper;
	@Override
	public List<Map<String, Double>> countReceiveByBusiId(String busiId,
			List<String> excludeItemCodes) {
		return cwIncomeExpensesItemMapper.countReceiveByBusiId(busiId, excludeItemCodes);
	}
	@Override
	public List<Map<String, Double>> countPaymentByBusiId(String busiId,
			List<String> includeItemCodes) {
		return cwIncomeExpensesItemMapper.countPaymentByBusiId(busiId, includeItemCodes);
	}
	@Override
	public void saveUpdateAndDelete(CwIncomeExpensesItem cwIncomeExpensesItem, OperateType operType) {
		switch (operType) {
		case add:
			cwIncomeExpensesItem.initInsertData(cwIncomeExpensesItem.getModifier(), cwIncomeExpensesItem.getModifierName());
			cwIncomeExpensesItemMapper.insert(cwIncomeExpensesItem);
			break;
		case edit:
			cwIncomeExpensesItem.initUpdateData(cwIncomeExpensesItem.getModifier(), cwIncomeExpensesItem.getModifierName());
			// 影响的记录数
			int updateCount = cwIncomeExpensesItemMapper.updateByPrimaryKeySelective(cwIncomeExpensesItem);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			cwIncomeExpensesItem.initDeleteData(cwIncomeExpensesItem.getModifier(), cwIncomeExpensesItem.getModifierName());
			// 影响的记录数
			int deleteCount = cwIncomeExpensesItemMapper.updateByPrimaryKeySelective(cwIncomeExpensesItem);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<CwIncomeExpensesItem> selectByExt(CwIncomeExpensesItemExt cwIncomeExpensesItemExt) {
		return cwIncomeExpensesItemMapper.selectByExt(cwIncomeExpensesItemExt);
	}
	
	@Override
	public int countByExt(CwIncomeExpensesItemExt cwIncomeExpensesItemExt){
		return cwIncomeExpensesItemMapper.countByExt(cwIncomeExpensesItemExt);
	}
	
	@Override
	public CwIncomeExpensesItem selectById(String id) {
		return cwIncomeExpensesItemMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			cwIncomeExpensesItemMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(CwIncomeExpensesItem cwIncomeExpensesItem) {
		return cwIncomeExpensesItemMapper.countRefined(cwIncomeExpensesItem);
	}

	@Override
	public CwIncomeExpensesItem selectRefined(CwIncomeExpensesItem cwIncomeExpensesItem) {
		return cwIncomeExpensesItemMapper.selectRefined(cwIncomeExpensesItem);
	}
	
	@Override
	public int deletedRefined(CwIncomeExpensesItem cwIncomeExpensesItem) {
		return cwIncomeExpensesItemMapper.deletedRefined(cwIncomeExpensesItem);
	}

	@Override
	public boolean repeat(CwIncomeExpensesItem cwIncomeExpensesItem) {
		int count = countRefined(cwIncomeExpensesItem);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(cwIncomeExpensesItem.getId())) {
			return true;
		}
		CwIncomeExpensesItem dbItem = selectRefined(cwIncomeExpensesItem);
		if(null == dbItem) {
			return false;
		}
		return !cwIncomeExpensesItem.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return cwIncomeExpensesItemMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<CwIncomeExpensesItem> cwIncomeExpensesItems) {
		return cwIncomeExpensesItemMapper.insertBatch(cwIncomeExpensesItems);
	}
}
